package edu.bu.cs673.superkids123.parents.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.common.model.Child;

import edu.bu.cs673.superkids123.common.model.Child.Age;
import edu.bu.cs673.superkids123.common.model.Child.Sex;
import edu.bu.cs673.superkids123.parents.controller.ChangeChildController;
import edu.bu.cs673.superkids123.parents.controller.CreateChildController;
import edu.bu.cs673.superkids123.parents.controller.DeleteChildController;
import edu.bu.cs673.superkids123.parents.controller.EditChildController;

public class DeleteChildView extends AbstractNavigationView implements
		KeyListener, ActionListener {

	private JTextField nameField;
	private JButton deleteBtn;
	private JLabel errorMessage;
	private JTextField ageField;
	private JTextField sexField;
	private Child current;
	// Start with false as these will be empty
	private boolean userNamePassed = false;

	public DeleteChildView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JPanel layout() {
		
		// TODO: load the child from the controller
		current=getMainFrame().getController(EditChildController.class)
						.loadChangeChild();
				
				// TODO: now that you have a child loaded in your private variable, you can fill in the form with this data now
				
		JPanel userInfo = new JPanel(new GridBagLayout());
		GridBagLayout gblUserInfo = new GridBagLayout();
		gblUserInfo.columnWidths = new int[] { 75, 100, 20, 300, 75 };
		gblUserInfo.rowHeights = new int[] { 50, 50, 50, 50, 50, 50 };
		userInfo.setLayout(gblUserInfo);

		JTextArea infoMsg = new JTextArea();
		infoMsg.setWrapStyleWord(true);
		infoMsg.setLineWrap(true);
		infoMsg.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		infoMsg.setEditable(false);
		infoMsg.setBackground(userInfo.getBackground());

		if (authController.areChildrenInitialized()) {
			infoMsg.setText("Do you really want to delete this child?");
		} else {
			infoMsg.setText("Do you really want to delete this child?");
		}

		GridBagConstraints gbcInfoMsg = new GridBagConstraints();
		gbcInfoMsg.insets = new Insets(0, 0, 5, 5);
		gbcInfoMsg.gridx = 0;
		gbcInfoMsg.gridy = 0;
		gbcInfoMsg.gridwidth = 5;
		gbcInfoMsg.fill = GridBagConstraints.HORIZONTAL;
		userInfo.add(infoMsg, gbcInfoMsg);

		JLabel nameLabel = new JLabel("Name:");
		nameLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		GridBagConstraints gbcNameLabel = new GridBagConstraints();
		gbcNameLabel.insets = new Insets(0, 0, 5, 5);
		gbcNameLabel.anchor = GridBagConstraints.EAST;
		gbcNameLabel.gridx = 1;
		gbcNameLabel.gridy = 1;
		userInfo.add(nameLabel, gbcNameLabel);

		nameField = new JTextField();
		nameLabel.setLabelFor(nameField);
		nameField.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		nameField.setText(current.getName());
		nameField.setEditable(false);
		nameField.addKeyListener(this);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.gridx = 3;
		gbc_textField.gridy = 1;
		userInfo.add(nameField, gbc_textField);
		nameField.setColumns(10);

		// Child Age
		JLabel ageLabel = new JLabel("Age ");
		ageLabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbcAgeNameLabel = new GridBagConstraints();
		gbcAgeNameLabel.insets = new Insets(0, 0, 5, 5);
		gbcAgeNameLabel.anchor = GridBagConstraints.EAST;
		gbcAgeNameLabel.gridx = 1;
		gbcAgeNameLabel.gridy = 2;
		userInfo.add(ageLabel, gbcAgeNameLabel);

		ageField = new JTextField();
		ageLabel.setLabelFor(ageField);
		ageField.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		String aux=" "+current.getAge();
		ageField.setText(aux);
		ageField.setEditable(false);
		ageField.addKeyListener(this);
		GridBagConstraints gbc_ageField = new GridBagConstraints();
		gbc_ageField.fill = GridBagConstraints.HORIZONTAL;
		gbc_ageField.insets = new Insets(0, 0, 5, 5);
		gbc_ageField.gridx = 3;
		gbc_ageField.gridy = 2;
		userInfo.add(ageField, gbc_ageField);

		// Sex
		JLabel sexLabel = new JLabel("Sex ");
		sexLabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbcSexLabel = new GridBagConstraints();
		gbcSexLabel.insets = new Insets(0, 0, 5, 5);
		gbcSexLabel.anchor = GridBagConstraints.EAST;
		gbcSexLabel.gridx = 1;
		gbcSexLabel.gridy = 3;
		userInfo.add(sexLabel, gbcSexLabel);

		sexField = new JTextField();
		nameLabel.setLabelFor(sexField);
		sexField.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		sexField.setText(current.getSex().toString());
		sexField.setEditable(false);
		sexField.addKeyListener(this);
		GridBagConstraints gbc_sexField = new GridBagConstraints();
		gbc_sexField.fill = GridBagConstraints.HORIZONTAL;
		gbc_sexField.gridx = 3;
		gbc_sexField.gridy = 3;
		userInfo.add(sexField, gbc_sexField);

		// Create Button
		JPanel createPanel = new JPanel(new BorderLayout());
		deleteBtn = new JButton("Delete");
		deleteBtn.setEnabled(true);
		deleteBtn.addActionListener(this);
		createPanel.add(deleteBtn, BorderLayout.EAST);

		errorMessage = new JLabel("");
		errorMessage.setForeground(Color.RED);
		createPanel.add(errorMessage, BorderLayout.WEST);

		GridBagConstraints gbcCreatePanel = new GridBagConstraints();
		gbcCreatePanel.anchor = GridBagConstraints.EAST;
		gbcCreatePanel.gridx = 3;
		gbcCreatePanel.gridy = 5;
		userInfo.add(createPanel, gbcCreatePanel);

		return userInfo;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source.equals(deleteBtn) && e.getActionCommand().equals("Delete")) {
			getMainFrame().getController(DeleteChildController.class)
					.deleteChild(current);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// Ignore this

	}

	@Override
	public void keyReleased(KeyEvent e) {
		Object source = e.getSource();
		if (source.equals(nameField)) {
			checkName();
		}

		// Check values
		if (userNamePassed) {
			deleteBtn.setEnabled(true);
		} else {
			deleteBtn.setEnabled(false);
		}

	}

	private void checkName() {
		String userName = nameField.getText();
		// Make sure field isn't empty
		if (!userName.equals("")) {
			errorMessage.setText("");
			userNamePassed = true;
			// Check if user already exists
		} else {
			errorMessage.setText("Username cannot be empty!");
			userNamePassed = false;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// Ignore this
	}

	@Override
	public String getTitle() {
		return "Delete Child Account";
	}

	@Override
	public String getBorderTitle() {
		return "Delete Child";
	}

}
